

#####################
### Load Packages ###
#####################

library(qdapRegex)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

CreatePlotBin <- function(CurrentFile, RR)
  {
  colnames(CurrentFile) <- c("X", "Y")
    Estimates <- subset(CurrentFile, CurrentFile$X!="=")
    
    CIs <- subset(CurrentFile, CurrentFile$X=="=")
      LowerBounds <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
      UpperBounds <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
    
    CurrentPlotBin <- data.frame(matrix(NA, 5,0))
      CurrentPlotBin$RR <- RR/100
      CurrentPlotBin$Outcome <- seq(-2,2,1)
      CurrentPlotBin$Estimate <- head(Estimates$Y,5)
        CurrentPlotBin$Estimate <- as.numeric(gsub("=", "", CurrentPlotBin$Estimate))
      CurrentPlotBin$LowerBound <- unlist(rm_between(CIs$Y, '=[', ',', extract=TRUE))
      CurrentPlotBin$UpperBound <- unlist(rm_between(CIs$Y, ',', ']', extract=TRUE))
   
    return(CurrentPlotBin)
  }
  
  RRLevel <- seq(0,100,10)
  
  for(i in 1:length(RRLevel))
  {
    CurrentFileName <- paste("Scripts/Figures/Estimates/Racial Resentment/StateLeg-Trust-RacialResentment-",RRLevel[i],".csv", sep="")
    CurrentFile <- read.csv(CurrentFileName)
    CurrentPlotBin <- CreatePlotBin(CurrentFile, RRLevel[i])  
    if(i==1){RunningPlotBin <- CurrentPlotBin}
    if(i>1){RunningPlotBin <- rbind(RunningPlotBin, CurrentPlotBin)}
  }

####
# Create Plots
###
  
CreatePlot <- function(CurrentData, Main)
  {
  
  customgrey <- rgb(150, 150, 150, alpha=80, maxColorValue=255, names="customgrey")
  
  plot(CurrentData$RR, CurrentData$Estimate,
       type="n",
       xlab="",
       ylab="",
       ylim=c(-.1,.1),
       xlim=c(0,1),
       family="B",
       xaxt="n",
       yaxt="n",
       yaxs = "i", # Removes Inner Margin
       xaxs = "i", # Removes Inner Margin
  )
  
  
  polygon(c(CurrentData$RR, rev(CurrentData$RR)), c(CurrentData$UpperBound, rev(CurrentData$LowerBound)), col=customgrey, border="NA")
  lines(CurrentData$RR, CurrentData$Estimate, lwd=2)
  abline(h=0)
  
  # Title
  title(main=Main, line=.5, cex.main=.75)
  
  # Axes
  axis(2, at=0, labels=c("Change in Predicted Probability"), line=1, cex.axis=.75, tick=FALSE, family="B")
  axis(2, at=c(-.1,-.05,0,.05,.1), labels=c("-0.10", "-0.05", "0.00", "0.05", "0.10"), cex.axis=.5, family="B", las=1)
 
  axis(1, at=c(0, .2,.4,.6,  .8,  1.0),
       labels=c("", "","","", "",""), cex.axis=.5, las=1,)
  
  axis(1, at=c(0, .2,.4,.6,  .8,  1.0),
       labels=c("0", "0.2","0.4","0.6", "0.8","1.0"), cex.axis=.5, las=1, line=-.75, tick=FALSE)
  
  axis(1, at=.5, labels=c("Racial Resentment Value"), line=.25, cex.axis=.75, tick=FALSE)
  }

  
  LowestResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==-2)
  LowResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==-1)
  NotSureResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==0)
  SomeResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==1)
  VeryResponsive <- subset(RunningPlotBin, RunningPlotBin$Outcome==2)
  
  windowsFonts(
    A=windowsFont("Arial Black"),
    B=windowsFont("Cambria"),
    C=windowsFont("Comic Sans MS"),
    D=windowsFont("Symbol")
  )

  tiff(filename = "Figures/Appendix/Figure-A-11.tiff", width = 7, height = 1.5, units = "in", pointsize = 12, res=1000)
  par(mfrow=c(1,5),
      mai=c(.3,.4,.25,.1),
      family="B")
      
      CreatePlot(LowestResponsive, "Never")
      CreatePlot(LowResponsive, "Some of the time")
      CreatePlot(NotSureResponsive, "About half of the time")
      CreatePlot(SomeResponsive, "Most of the time")
      CreatePlot(VeryResponsive, "Always")
    dev.off()
    
    


